package com.insurance.cis.views;

import com.insurance.cis.controllers.StatisticsReportController;
import com.insurance.cis.daos.CampaignDao;
import com.insurance.cis.models.Campaign;
import com.insurance.cis.scos.CampaignSearchCondition;
import com.insurance.cis.utils.CisConstants;
import com.insurance.cis.utils.HtmlReportUtils;
import com.insurance.cis.utils.MessageDialogUtils;
import com.insurance.cis.utils.Utils;
import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author TruongLQ
 */
public class StatisticsReportJPanelView extends javax.swing.JPanel {

    final CampaignDao campaignDao = new CampaignDao();
    final Pattern pattern = Pattern.compile(CisConstants.DATE_PATTERN_COMPILE);
    DefaultTableModel dtm;
    MainJFrameView mainJFrameView;
    StatisticsReportController controller = new StatisticsReportController(this);

    /**
     * Creates new form StatisticsReportJPanelView
     */
    public StatisticsReportJPanelView(javax.swing.JFrame jFrame) {
        initComponents();
        mainJFrameView = (MainJFrameView) jFrame;
        loadTableMetaData();
    }

    private void loadTableMetaData() {
        Vector colHeader = new Vector(14);
        colHeader.add("Campaign ID");
        colHeader.add("Campaign Name");
        colHeader.add("Product Name");
        colHeader.add("Channel Desc");
        colHeader.add("Discount");
        colHeader.add("Gift");
        colHeader.add("Estimated Budget");
        colHeader.add("Actual Budget");
        colHeader.add("Budget variance");
        colHeader.add("Estimated Sale");
        colHeader.add("Actual Sale");
        colHeader.add("Sale variance");
        colHeader.add("Start Date");
        colHeader.add("End Date");
        dtm = new DefaultTableModel(colHeader, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        tblCampaignResult.setModel(dtm);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        txtCampaignID = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtProductID = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtStartDate = new javax.swing.JTextField();
        txtEndDate = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCampaignResult = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        rbtnModeAll = new javax.swing.JRadioButton();
        rbtnModeSelected = new javax.swing.JRadioButton();
        jPanel4 = new javax.swing.JPanel();
        rbtnDifferentCampaigns = new javax.swing.JRadioButton();
        rbtnVariances = new javax.swing.JRadioButton();
        rbtnSalesTarget = new javax.swing.JRadioButton();
        rbtnDefault = new javax.swing.JRadioButton();
        btnHTMLReport = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Statistics report");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Search criteria", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        jLabel2.setText("Campaign ID");

        jLabel3.setText("Product ID");

        jLabel4.setText("Start date");

        jLabel5.setText("End date");

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(txtProductID))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(txtCampaignID, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(40, 40, 40)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel2, jLabel3});

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel4, jLabel5});

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnReset, btnSearch});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtCampaignID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(txtStartDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtProductID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txtEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSearch)
                    .addComponent(btnReset))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblCampaignResult.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblCampaignResult);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Report", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Mode", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        buttonGroup1.add(rbtnModeAll);
        rbtnModeAll.setText("All");

        buttonGroup1.add(rbtnModeSelected);
        rbtnModeSelected.setText("Selected item");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbtnModeAll)
                    .addComponent(rbtnModeSelected))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(rbtnModeAll)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtnModeSelected)
                .addGap(0, 36, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Exported data", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        buttonGroup2.add(rbtnDifferentCampaigns);
        rbtnDifferentCampaigns.setText("Different campaigns for different products");

        buttonGroup2.add(rbtnVariances);
        rbtnVariances.setText("Variances reports (estimated vs. actuals)");

        buttonGroup2.add(rbtnSalesTarget);
        rbtnSalesTarget.setText("Sales target reports");

        buttonGroup2.add(rbtnDefault);
        rbtnDefault.setText("Default");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbtnDefault)
                    .addComponent(rbtnSalesTarget)
                    .addComponent(rbtnVariances)
                    .addComponent(rbtnDifferentCampaigns))
                .addContainerGap(76, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(rbtnDifferentCampaigns)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtnVariances)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtnSalesTarget)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtnDefault))
        );

        btnHTMLReport.setText("HTML Report");
        btnHTMLReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHTMLReportActionPerformed(evt);
            }
        });

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(51, 51, 51)
                .addComponent(btnHTMLReport)
                .addGap(18, 18, 18)
                .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnHTMLReport)
                        .addComponent(btnClose))
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(46, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(33, 33, 33))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        txtCampaignID.setText("");
        txtEndDate.setText("");
        txtProductID.setText("");
        txtStartDate.setText("");
    }//GEN-LAST:event_btnResetActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        CampaignSearchCondition sco = controller.validateSearchConditions();
        if (sco == null) {
            return;
        }
        List<Campaign> campaigns = campaignDao.searchCampaigns(sco);
        if (!campaigns.isEmpty()) {
            loadResults(campaigns);
        } else {
            MessageDialogUtils.dialogMessage(this, "No campaigns found");
            dtm.getDataVector().clear();
            tblCampaignResult.updateUI();
        }

    }//GEN-LAST:event_btnSearchActionPerformed

    private void btnHTMLReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHTMLReportActionPerformed
        
        if (!controller.isOkReport()) {
            return;
        }
        final String filename = "Campaign_HTML_Report.html";
        try {
            try (BufferedWriter bw = new BufferedWriter(
                         new FileWriter(filename))) {
                bw.write("<html>");
                bw.write("<body>");
                generateTableMetadata(bw);
                bw.write("<table style='border:1px solid red;border-collapse:collapse;'>");
                if (rbtnModeAll.isSelected()) {
                    exportTitle(bw);
                    int rowCount = tblCampaignResult.getRowCount();
                    for (int row = 0; row < rowCount; row++) {
                        exportSelectedRow(row, bw);
                        bw.write("\n");
                    }
                } else {
                    int row = tblCampaignResult.getSelectedRow();
                    if (row < 0 || row >= tblCampaignResult.getRowCount()) {
                        MessageDialogUtils.dialogError(this, "You must choose one item to to export!");
                        return;
                    }
                    exportTitle(bw);
                    exportSelectedRow(row, bw);
                }
                bw.write("</table>");
                bw.write("</body>");
                bw.write("</html>");
                bw.close();
            }
            
        } catch (IOException ex) {
            Logger.getLogger(StatisticsReportJPanelView.class.getName()).log(Level.SEVERE, null, ex);
        }

        MessageDialogUtils.dialogMessage(this, "Report " + filename + " successful");
        try {
            Desktop.getDesktop().open(new File(filename));
        } catch (IOException ex) {
        }
    }//GEN-LAST:event_btnHTMLReportActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        mainJFrameView.dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    void generateTableMetadata(BufferedWriter bw) {
        try {
            bw.write("<table style='border:1px solid red;border-collapse:collapse;margin-bottom: 20px;background-color:gray;color:white;'");
            bw.write("<tr>");
            bw.write("<td>");
            bw.write("Automatically generated from ");
            bw.write("</td>");
            bw.write("<td>");
            bw.write(" : CIS application");
            bw.write("</td>");
            bw.write("</tr>");
            bw.write("<tr>");
            bw.write("<td>");
            bw.write("Date ");
            bw.write("</td>");
            bw.write("<td>");
            bw.write(" : "+Utils.formatWithHHMMSS(new Date(), CisConstants.DATE_DD_MM_YYYY));
            bw.write("</td>");
            bw.write("</tr>");
            bw.write("<tr>");
            bw.write("<td>");
            bw.write("Report type ");
            bw.write("</td>");
            bw.write("<td>");
            if (rbtnDifferentCampaigns.isSelected()) {
                bw.write(" : Different campaigns for different products");
            } else if (rbtnVariances.isSelected()) {
                bw.write(" : Variance reports (estimated vs. actuals)");
            } else if (rbtnSalesTarget.isSelected()) {
                bw.write(" : Sales target");
            } else {
                bw.write(" : All campaign details");
            }
            bw.write("</td>");
            bw.write("</tr>");
            bw.write("</table>");
        } catch (IOException ex) {
            Logger.getLogger(StatisticsReportJPanelView.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    void exportTitle(BufferedWriter bw) {
        if (rbtnDifferentCampaigns.isSelected()) {
            HtmlReportUtils.exportTitleDifferentCampaigns(bw);
        } else if (rbtnVariances.isSelected()) {
            HtmlReportUtils.exportTitleVariances(bw);
        } else if (rbtnSalesTarget.isSelected()) {
            HtmlReportUtils.exportTitleSalesTarget(bw);
        } else {
            HtmlReportUtils.exportTitleDefault(bw);
        }
    }

    void exportSelectedRow(int row, BufferedWriter bw) {
        if (rbtnDifferentCampaigns.isSelected()) {
            HtmlReportUtils.exportDifferentCampaigns(row, bw, dtm);
        } else if (rbtnVariances.isSelected()) {
            HtmlReportUtils.exportVariances(row, bw, dtm);
        } else if (rbtnSalesTarget.isSelected()) {
            HtmlReportUtils.exportSalesTarget(row, bw, dtm);
        } else {
            HtmlReportUtils.exportDefault(row, bw, dtm);
        }
    }

    private void loadResults(List<Campaign> campaigns) {
        dtm.getDataVector().clear();
        for (Campaign campaign : campaigns) {
            dtm.addRow(new Object[]{
                        campaign.getCampaignID(),
                        campaign.getCampaignName(),
                        campaign.getProduct().getName(),
                        campaign.getChannel().getChannelDesc(),
                        campaign.getPromotion().getDiscount(),
                        campaign.getPromotion().getGift(),
                        campaign.getEstimatedBudget(),
                        campaign.getActualBudget(),
                        campaign.getBudgetVariance(),
                        campaign.getEstimatedSale(),
                        campaign.getActualSale(),
                        campaign.getSaleVariance(),
                        campaign.getStartDateStr(),
                        campaign.getEndDateStr()
                    });
        }
    }
    
    public void clearAllTextFields() {
        btnResetActionPerformed(null);
        dtm.getDataVector().clear();
        buttonGroup1.clearSelection();
        buttonGroup2.clearSelection();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnHTMLReport;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSearch;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JRadioButton rbtnDefault;
    private javax.swing.JRadioButton rbtnDifferentCampaigns;
    private javax.swing.JRadioButton rbtnModeAll;
    private javax.swing.JRadioButton rbtnModeSelected;
    private javax.swing.JRadioButton rbtnSalesTarget;
    private javax.swing.JRadioButton rbtnVariances;
    private javax.swing.JTable tblCampaignResult;
    private javax.swing.JTextField txtCampaignID;
    private javax.swing.JTextField txtEndDate;
    private javax.swing.JTextField txtProductID;
    private javax.swing.JTextField txtStartDate;
    // End of variables declaration//GEN-END:variables

    public JTextField getTxtCampaignID() {
        return txtCampaignID;
    }

    public JTextField getTxtEndDate() {
        return txtEndDate;
    }

    public JTextField getTxtProductID() {
        return txtProductID;
    }

    public JTextField getTxtStartDate() {
        return txtStartDate;
    }

    public JRadioButton getRbtnDefault() {
        return rbtnDefault;
    }

    public JRadioButton getRbtnDifferentCampaigns() {
        return rbtnDifferentCampaigns;
    }

    public JRadioButton getRbtnModeAll() {
        return rbtnModeAll;
    }

    public JRadioButton getRbtnModeSelected() {
        return rbtnModeSelected;
    }

    public JRadioButton getRbtnSalesTarget() {
        return rbtnSalesTarget;
    }

    public JRadioButton getRbtnVariances() {
        return rbtnVariances;
    }
}
